﻿using Common.Extension;
using Common.Json;
using System;
using System.Collections.Generic;
using System.Text;

namespace Common.MQ
{
    /// <summary>
    /// 消息model
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class MessageModel<T> : IMessageModel
    {
        private readonly string destination;
        private readonly MessageProperty msgProperty;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="destination"></param>
        /// <param name="data"></param>
        /// <param name="property"></param>
        public MessageModel(string destination, T data, MessageProperty property = null)
        {
            this.destination = destination;
            this.Data = data;
            this.msgProperty = property;
        }
        /// <summary>
        /// 消息内容
        /// </summary>
        public T Data { get; set; }
        /// <summary>
        /// 消息目的地
        /// </summary>
        public string GetDestination()
        {
            return this.destination;
        }
        /// <summary>
        /// 获取消息json
        /// </summary>
        /// <returns></returns>
        public string GetMessageJson()
        {
            var dict = new Dictionary<string, object>();
            //remove _TypeString next version
            dict.Add("_TypeString", GetMessageType());
            Data.ToDictionary(dict);
            return JsonSerializer.JsonSerialize(dict);
        }
        /// <inheritdoc/>
        public string GetMessageType()
        {
            return typeof(T).FullName;
        }

        /// <inheritdoc/>
        public MessageProperty GetProperties()
        {
            return msgProperty;
        }
    }
}
